package com.ykdl.member.kid.adapters;

import java.util.ArrayList;
import java.util.List;

import com.ykdl.member.constant.KidAction;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.Coupon_New_FragmentAdapter.ViewHolder;
import com.ykdl.member.kid.adapters.Coupon_New_FragmentAdapter.onCheckListener;
import com.ykdl.member.kid.beans.OnLineCouponBean;
import com.ykdl.member.kid.beans.TicketBean;
import com.ykdl.member.kid.marketcard.CouponinfoWebView;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
/********************************************************************************

** 作者： jiafeiyu
** 创始时间：2015-7-24
** 描述：
**    门店货架之优惠券之在线可用优惠券
 * @param <E>

*********************************************************************************/
public class OnlineCouponAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater inflater;
	private List<OnLineCouponBean> mOnLineList;
	private List<Integer> mCheckedList;
	private String store_id;
	
	public OnlineCouponAdapter(Context context,onCheckListener mSelectListener) {
		mContext = context;
		inflater =LayoutInflater.from(mContext);
		this.mSelectListener = mSelectListener;
	}
	
	@Override
	public int getCount() {
		if (mOnLineList!=null&&mOnLineList.size()>0) {
			return mOnLineList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolderOnLine holder;
		if (convertView==null) {
			holder = new ViewHolderOnLine();
			convertView = inflater.inflate(R.layout.item_online_coupon, null);
			holder.tv_online_coupon_number = (TextView) convertView.findViewById(R.id.tv_online_coupon_number);
			holder.tv_coupon_new_type = (TextView) convertView.findViewById(R.id.tv_coupon_new_type);
			holder.tv_coupon_new_xilie = (TextView) convertView.findViewById(R.id.tv_coupon_new_xilie);
			holder.tv_online_coupon_bt = (TextView) convertView.findViewById(R.id.tv_online_coupon_bt);
			holder.rl_item_online_pay = (RelativeLayout) convertView.findViewById(R.id.rl_item_online_pay);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolderOnLine) convertView.getTag();
		}
		OnLineCouponBean info = mOnLineList.get(position);
		
		if (info.isReceive_flag()||mCheckedList.contains(position)) {
			holder.tv_online_coupon_bt.setText("已领取");
			holder.tv_online_coupon_bt.setTextColor(mContext.getResources().getColor(R.color.mycoupon_gray));
			holder.rl_item_online_pay.setBackgroundResource(R.drawable.item_coupon_online_bg);
		}else {
			holder.tv_online_coupon_bt.setText("立即领取");
			holder.tv_online_coupon_bt.setTextColor(mContext.getResources().getColor(R.color.coupon_red));
			holder.rl_item_online_pay.setBackgroundResource(R.drawable.item_coupon_online);
		}
		holder.tv_online_coupon_bt.getPaint().setFakeBoldText(true); 
		switch (info.getCoupon_type()) {
		case 4:
			holder.tv_coupon_new_type.setText("代金券");
			break;

		default:
			break;
		}
		if (!TextUtils.isEmpty(info.getScope_desc())) {
			holder.tv_coupon_new_xilie.setText(info.getScope_desc());
		}
		holder.tv_online_coupon_number.setText(info.getCoupon_num_1());
		holder.rl_item_online_pay.setTag(info);
		holder.rl_item_online_pay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 页面详情的跳转
				
				OnLineCouponBean info = (OnLineCouponBean) v.getTag();
				Intent intent = new Intent(mContext, CouponinfoWebView.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KidAction.COUPON_ID, info.getCoupon_id()+"");
				intent.putExtra(KidAction.STORE_ID, store_id);
				intent.putExtra(KidAction.URL_TYPE, 8);
				mContext.startActivity(intent);
			}
		});
		holder.tv_online_coupon_bt.setTag(position);
		holder.tv_online_coupon_bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 领取的点击事件
				
				if (mSelectListener!=null) {
					int index = (Integer) v.getTag();
					
					OnLineCouponBean info = mOnLineList.get(index);
					
					if (!info.isReceive_flag()&&!mCheckedList.contains(index)) {
						
						mSelectListener.onCheckListener(holder, info, position);
					}else {
						Toast.makeText(mContext, "已领取，无需再领", 0).show();
					}
				}
			}
		});
		return convertView;
	}

	/**
	 * 初始化 在线支付可用的优惠券
	 * @param position
	 * @param convertView
	 * @return
	 */
	public class ViewHolderOnLine{
		TextView tv_online_coupon_number;
		TextView tv_coupon_new_type;
		TextView tv_coupon_new_xilie;
		TextView tv_online_coupon_bt;
		RelativeLayout rl_item_online_pay;
	}
	/**
	 * 在线支付可用优惠券的领取事件
	 * @author jiafeiyu
	 *
	 */
	private onCheckListener mSelectListener;
	public interface onCheckListener{
		boolean onCheckListener(ViewHolderOnLine holder, OnLineCouponBean bean,int position);
	}
	public void setData(List<OnLineCouponBean> tickets) {
		mOnLineList = tickets;
	}
	
	/**
	 * 用于记录是否已经添加了
	 * @param mCheckedList
	 */
	public void setCheckedList(List<Integer> mCheckedList){
		this.mCheckedList = mCheckedList;
		if (mCheckedList==null) {
			mCheckedList = new ArrayList<Integer>();
		}
	}

	public void setStore_id(String store_id) {
		this.store_id = store_id;
	}
	
	
}
